package com.example.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.demo.R;
import com.example.demo.api.ApiService;
import com.example.demo.api.ResponseData;
import com.example.demo.api.RetrofitClient;
import com.example.demo.request.LoginRequest;
import com.example.demo.utils.LogUtils;
import com.example.demo.utils.TokenManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {


    private EditText etUsername, etPassword;
    private Button btnLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);

        etUsername.setText("admin");
        etPassword.setText("admin123");

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();


                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                }

                LoginRequest loginRequest = new LoginRequest(username, password);

                ApiService service = RetrofitClient.createService(ApiService.class);
                Call<ResponseData> login = service.login(loginRequest);

                login.enqueue(new Callback<ResponseData>() {
                    @Override
                    public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                        if (response.isSuccessful()) {
                            TokenManager tokenManager = new TokenManager(LoginActivity.this);

                            tokenManager.saveToken(response.body().getToken());

                            LogUtils.d("=====>login", response.body().toString());


                            if (response.body().getToken() != null || !response.body().getToken().isEmpty()) {
                                // 登录成功
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                                // 登录成功后跳转到首页
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();  // 结束当前页面，防止返回到登录页面
                            }


                        } else {
                            // 登录失败
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseData> call, Throwable t) {
                        // 网络请求失败
                        Toast.makeText(LoginActivity.this, t.getMessage().toString(), Toast.LENGTH_SHORT).show();
                    }
                });


            }
        });


    }
}